<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h2></h2>
<button>点击停止定时器</button>

<script>

    const btn = document.querySelector('button');
    const h2 = document.querySelector('h2');

    h2.innerHTML = new Date().toLocaleDateString();

    let time = 5000;
    let flag = false;

    const timer001 = setTimeout(function () {
        console.log('爆炸了')
        btn.innerHTML = '定时器爆炸了!!!';
        flag = true;
    }, time);

    btn.addEventListener('click', function () {
        if (!flag) {
            window.clearTimeout(timer001);
            btn.innerHTML = '爆炸已经被停止 ^_^';
        } else {
            alert('定时器已经保证了, 停止没有用')
        }
    })
</script>
</body>
</html>
